package com.sscaipiao.android.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.sscaipiao.android.R;
import com.sscaipiao.android.model.MyLottery;
import com.sscaipiao.android.model.TogetherBuy;
import com.sscaipiao.android.utils.StringUtil;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class WoDeItemAdapt extends BaseAdapter {

	private List<MyLottery> mArraylist;
	private Context context;
	public  List<MyLottery> mOrderedlist;
	private int mOrderKey;

	public WoDeItemAdapt(Context c, List<MyLottery> list) {
		this.context = c;
		this.mArraylist = list;
//		mOrderedlist = sortHeMaiListByKey(ORDERBY_CanYuJinDu,ORDER_DOWN);

	}
	public void setOrderKey(int key, int order){
		mOrderKey = key;
//		mOrderedlist = sortHeMaiListByKey(mOrderKey,order);
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {

		return mArraylist.size();
	}

	@Override
	public Object getItem(int arg0) {

		return mArraylist.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
	static class ViewHolder {
		public ImageView lottery_image;
		public TextView tv_name;
		public TextView tv_season;
		public TextView tv_buy;
		public TextView tv_money;
		public TextView tv_time;
		public TextView tv_zhongjiang;
	}
	@Override
	public View getView(int arg0, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = (View) LayoutInflater.from(context).inflate(R.layout.together_hall_item, null);
			holder = new ViewHolder();
			holder.lottery_image = (ImageView) convertView.findViewById(R.id.lottery_image);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_season = (TextView) convertView.findViewById(R.id.tv_season);
			holder.tv_buy = (TextView) convertView.findViewById(R.id.tv_buy);
			holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_zhongjiang = (TextView) convertView.findViewById(R.id.tv_zhongjiang);
			convertView.setTag(holder);
		}   else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (mOrderedlist.size() == 0) {
			return convertView;
		}
		MyLottery item = mOrderedlist.get(arg0);
		if (item != null) {
			//mOrderedlist = getDataByKey(mOrderKey,true);
			holder.lottery_image.setImageResource(item.getImgID());
			holder.tv_name.setText(item.getLotteryName());
			holder.tv_season.setText(item.getPeriodNo());
			//holder.tv_buy.setText(item.());
			holder.tv_money.setText(item.getBetMoney());
			holder.tv_time.setText(item.getBetTime());
			holder.tv_zhongjiang.setText(item.getAwardInfo());

		}


		return convertView;

	}
	private List<MyLottery> sortHeMaiListByKey(final int key, final int up) {
		if (mArraylist.isEmpty()) {
			return mArraylist;
		}
		try {
			Collections.sort(mArraylist, new Comparator<MyLottery>() {
				@Override
				public int compare(MyLottery object1, MyLottery object2) {
//					if (key == ORDERBY_CanYuJinDu) {
//						if (up == ORDER_UP) {
//							return (((Integer)Integer.valueOf(object1.getPercent())).compareTo((Integer)Integer.valueOf( object2.getPercent())));
//						} else {
//							return (((Integer)Integer.valueOf(object2.getPercent())).compareTo((Integer)Integer.valueOf( object1.getPercent())));
//						}
//					}
//					if (key == ORDERBY_FangAnZongE) {
//						if (up == ORDER_UP) {
//							return (((Integer)Integer.valueOf(object1.getBetMoney())).compareTo((Integer)Integer.valueOf( object2.getBetMoney())));
//						} else {
//							return (((Integer)Integer.valueOf(object2.getBetMoney())).compareTo((Integer)Integer.valueOf( object1.getBetMoney())));
//						}
//					}
//					if (key == ORDERBY_MeiFenJinE) {
//						if (up == ORDER_UP) {
//							return (((Integer)Integer.valueOf(object1.getPrice())).compareTo((Integer)Integer.valueOf( object2.getPrice())));
//						} else {
//							return (((Integer)Integer.valueOf(object2.getPrice())).compareTo((Integer)Integer.valueOf( object1.getPrice())));
//						}
//					}
//					if (key == ORDERBY_FanJiagLv) {
//						if (up == ORDER_UP) {
//							return (((Integer)Integer.valueOf(object1.getPercent())).compareTo((Integer)Integer.valueOf( object2.getPercent())));
//						} else {
//							return (((Integer)Integer.valueOf(object2.getPercent())).compareTo((Integer)Integer.valueOf( object1.getPercent())));
//						}
//					}
					return 0;
				}
			});
		} catch (Exception e) {

		}

		return mArraylist;
	}

}
